<template>
  <el-container class="container">
    <!-- 顶部导航栏 -->
    <el-header>
      <div class="header_items">
        <div class="header_item1">
          <img src="../assets/logo.png" alt="logo" />
        </div>
        <div class="header_item2">
          <span>网易云音乐</span>
        </div>
        <div class="other_item">
          <div
            v-for="(item, index) in menuList"
            :key="index"
            :class="{ selected: item.isActive }"
            @click="changePage(index)"
          >
            {{ item.itemName }}
          </div>
        </div>
      </div>
    </el-header>
    <!-- 主体内容区 -->
    <el-main>
      <router-view></router-view>
    </el-main>
    <!-- 播放条区域 -->
    <el-footer>
      <Player />
    </el-footer>
    <!-- 解决覆盖问题 -->
    <div id="occupy"></div>
  </el-container>
</template>

<script>
import Player from "@/components/Home/Player";
export default {
  name: "Home",
  components: {
    Player,
  },
  data() {
    return {
      // 菜单栏数据
      menuList: [
        {
          itemName: "音乐厅",
          isActive: true,
        },
        {
          itemName: "歌单",
          isActive: false,
        },
        {
          itemName: "歌手",
          isActive: false,
        },
        {
          itemName: "MV",
          isActive: false,
        },
        {
          itemName: "个人中心",
          isActive: false,
        },
      ],
      // 路由数据
      routeData: [
        "/concerthall",
        "/songlist",
        "/artist",
        "/musicvideo",
        "/personal",
      ],
      // 当前路径索引
      index: 0,
    };
  },
  methods: {
    // 修改选中状态
    changActive(index) {
      this.menuList.forEach((item) => {
        if (item.isActive) {
          item.isActive = false;
        }
      });
      this.menuList[index].isActive = true;
    },
    // 切换菜单
    changePage(index) {
      if (index == this.index) {
        return;
      }
      this.changActive(index);
      this.$router.replace({ path: this.routeData[index] });
      // 将索引保存至本地
      window.sessionStorage.setItem("index", index);
      this.index = window.sessionStorage.getItem("index");
    },
  },
  mounted() {
    if (window.sessionStorage.getItem("index") !== null) {
      this.index = window.sessionStorage.getItem("index");
    }
    this.changActive(this.index);
  },
};
</script>

<style lang="less" scoped>
.container {
  height: 100%;
  #occupy{
    height: 60px;
  }
}
// 导航栏部分
.el-header {
  background-color: rgba(36, 36, 36, 0.9);
  box-shadow: 0 2px 20px rgb(207, 143, 143);
  z-index: 1;
  .header_items {
    display: flex;
    height: 100%;
    .header_item1 {
      margin-left: 80px;
      img {
        transform: translateY(12px);
        height: 60%;
      }
    }
    .header_item2 {
      margin-left: 12px;
      display: flex;
      align-items: center;
      span {
        color: #fff;
        font-size: 20px;
        user-select: none;
      }
    }
    .other_item {
      width: 50%;
      display: flex;
      margin-left: 60px;
      font-size: 16px;
      color: #ddd;
      line-height: 60px;
      justify-content: space-around;
      div {
        cursor: pointer;
        user-select: none;
      }
      div:hover {
        color: #fff;
      }
    }
  }
}

// 主体内容区
.el-main {
  background-color: #eee;
  color: #333;
  padding: 0;
}

// 页脚部分
.el-footer {
  background-color: #ddd;
  position: fixed !important;
  bottom: 0;
  width: 100%;
  padding: 0;
}
// 选中效果
.selected {
  border-bottom: 2.58px solid #ffffff;
  color: #fff;
  font-weight: bold;
}
</style>